//package ca.uwo.android.rss;
//
//import java.io.InputStream;
//
//import android.sax.Element;
//import android.sax.EndElementListener;
//import android.sax.EndTextElementListener;
//import android.sax.RootElement;
//import android.util.Log;
//import android.util.Xml;
//
//public class MustangsScheduleParser extends SaxEventCalendarParser {
//	
//
//	/**
//	 * Used to tag log messages from this class
//	 */
//	private static final String TAG_NAME = "MustangsScheduleParser";
//
//	/**
//	 * Constructor for the rss feed parser
//	 * 
//	 * @param rssFeedURL
//	 *            - the url of the rss feed to parse
//	 */
//	public MustangsScheduleParser(String rssFeedURL) throws Exception {
//		super(rssFeedURL);
//
//	}
//
//	/**
//	 * This method parses the rss feed and returns an rss feed object containing
//	 * all the feed items read in from the source
//	 * 
//	 * @return
//	 */
//	public RssFeed parse() throws Exception {
//		
//		Log.d(TAG_NAME, "In MustangsScheduleParser parse() method");
//		final RssItem currentItem = new RssItem(); // represents current item
//													// being parsed
//
//		// define the root element of the rss feed document
//		RootElement root = new RootElement(RSS);
//
//		final RssFeed rssFeed = new RssFeed(); // rss feed object to store all
//												// the rss items
//
//		String rssFeedText;
//
//		InputStream is = this.getInputStream(); // get the input stream to the
//												// rss feed
//		rssFeedText = this.convertSpecialChars(is); // convert special
//													// characters in feed
//		Log.d(TAG_NAME, "Rss feed text = " + rssFeedText);
//
//		// get each (child) item element from the root and store it in the rss
//		// feed object
////		Element item = root.getChild(ITEM);
//		Element item = root.getChild(RSS_NAMESPACE, ITEM);
//		Log.d(TAG_NAME, "Got item: " + item);
//		item.setEndElementListener(new EndElementListener() {
//			public void end() {
//				Log.d(TAG_NAME, "Got item");
//				rssFeed.add(currentItem.copy());
//			}
//		});
//
//		// parse and store the title of the rss feed item
//		item.getChild(TITLE).setEndTextElementListener(
//				new EndTextElementListener() {
//					public void end(String body) {
//						Log.d(TAG_NAME, "Got title " + body);
//						currentItem.setTitle(body);
//					}
//				});
//
//		// parse and store the link of the rss feed item
//		item.getChild(LINK).setEndTextElementListener(
//				new EndTextElementListener() {
//					public void end(String body) {
//						Log.d(TAG_NAME, "Got link " + body);
//						currentItem.setLink(body);
//					}
//				});
//
//		// parse and store the description of the rss feed item
//		item.getChild(DESCRIPTION).setEndTextElementListener(
//				new EndTextElementListener() {
//					public void end(String body) {
//						Log.d(TAG_NAME, "Got description " + body);
//						currentItem.setDescription(body);
//					}
//				});
//
//
//		// for the Mustangs feed, there is no date set, just a startdate, which
//		// will be used instead
//		item.getChild(EV_NAMESPACE, STARTDATE).setEndTextElementListener(
//				new EndTextElementListener() {
//					public void end(String body) {
//						Log.d(TAG_NAME,
//								"Got an ev:startdate of " + body);
//						currentItem.setDate(body);
//					}
//				});
//
//		Log.i(TAG_NAME, "About to parse feed...");
//		
//		// parse the rss feed as a string (since the special characters had to
//		// be replaced)
//		Xml.parse(rssFeedText, root.getContentHandler());
//
//		Log.i(TAG_NAME, "Feed parsed...");
//		
//		return rssFeed;
//	}
//
//}

/**
 * This class represents a custom rss feed parser which is used to parse
 * rss feeds for all rss feeds except the events calendar module in the 
 * iWestern application. It is a subclass of RssFeedParser and uses 
 * methods / attributes from this class when parsing the rss feed.
 * @author Chirag Mistry
 */

package ca.uwo.android.mustangs;

import java.io.InputStream;

import ca.uwo.android.rss.RssFeed;
import ca.uwo.android.rss.RssFeedParser;
import ca.uwo.android.rss.RssItem;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Log;
import android.util.Xml;


/**
 * The Mustangs schedule is different than the other RSS feeds, this is a sub-class of RssFeedParser with the parse
 * @author tracyg
 *
 */
public class MustangsScheduleParser extends RssFeedParser {

	protected static final String EV_NAMESPACE = "http://purl.org/rss/1.0/modules/event/";

	protected static final String RSS_NAMESPACE = "http://purl.org/rss/2.0/";

	protected static final String STARTDATE = "startdate";

	protected static final String LOCATION = "location";

	/**
	 * Used to tag log messages from this class
	 */
	private static final String TAG_NAME = "SaxRssParser";

	/**
	 * Constructor for the rss feed parser
	 * 
	 * @param rssFeedURL
	 *            - the url of the rss feed to parse
	 */
	public MustangsScheduleParser(String rssFeedURL) throws Exception {
		super(rssFeedURL);
	}

	/**
	 * This method parses the rss feed and returns an rss feed object containing
	 * all the feed items read in from the source
	 * 
	 * @return
	 */
	public RssFeed parse() throws Exception {

		//Log.d(TAG_NAME, "In MustangsScheduleParser parse() method");

		final RssItem currentItem = new RssItem(); // represents current item
													// being parsed
		// set a default date
		currentItem.setDate("Thu, 08 Sep 2011 04:00:00 GMT");
		RootElement root = new RootElement(RSS); // define the root element of
													// the rss feed document

		final RssFeed rssItems = new RssFeed(); // rss feed object to store all
												// rss items

		Element channel = root.getChild(CHANNEL); // get the channel element
													// from root

		String rssFeedText;

		InputStream is = this.getInputStream(); // get the input stream to the
												// rss feed
		rssFeedText = convertSpecialChars(is); // convert special characters in
												// feed

		// get each (child) item element from the channel and store it in the
		// rss feed object
		Element item = channel.getChild(ITEM);
		item.setEndElementListener(new EndElementListener() {
			public void end() {
				//Log.d(TAG_NAME, "Finished getting item");
				rssItems.add(currentItem.copy());
			}
		});

		// parse and store the title of the rss feed item
		item.getChild(TITLE).setEndTextElementListener(

		new EndTextElementListener() {
			public void end(String body) {
				//Log.d(TAG_NAME, "Got title");
				currentItem.setTitle(body);
			}
		});

		// parse and store the link of the rss feed item
		item.getChild(LINK).setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						//Log.d(TAG_NAME, "Got link");
						currentItem.setLink(body);
					}
				});

		// parse and store the description of the rss feed item
		item.getChild(DESCRIPTION).setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						//Log.d(TAG_NAME, "Got desc");
						if (body.indexOf("<img src") != -1) {
							// removes <img src=""> tags from description (for
							// mustang news)
							body = body.replaceAll(
									"\\x3Cimg src{1}.*\\x2F\\x3E", "");
						}
						currentItem.setDescription(body);

					}
				});

		// for the Mustangs feed, there is no date set, just a startdate, which
		// will be used instead
		item.getChild(EV_NAMESPACE, STARTDATE).setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						//Log.d(TAG_NAME, "Got an ev:startdate of " + body);
						// remove time from date field
						if (body != null)
							body = body.substring(0, body.indexOf("T"));
						currentItem.setDate(body);
					}
				});

		// for the Mustangs feed, there is no date set, just a startdate, which
		// will be used instead
		item.getChild(EV_NAMESPACE, LOCATION).setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						//Log.d(TAG_NAME, "Got an ev:location of " + body);
						currentItem.setLocation(body);
					}
				});

		// parse the rss feed directly from the input stream using utf 8
		// encoding
		Xml.parse(rssFeedText, root.getContentHandler());

		//Log.d(TAG_NAME, "Finished parse method, returning number of items: "
//				+ rssItems.size());
		return rssItems;
	}
}
